#ifndef FLOW_INSPECTOR_H_
#define FLOW_INSPECTOR_H_

#include <string>
#include <unordered_map>

#include "muduo/net/EventLoop.h"
#include "muduo/net/InetAddress.h"
#include "muduo/net/TcpClient.h"
#include "muduo/base/Logging.h"
#include "libcurlwrappersmtp.hpp"

using namespace muduo;
using namespace muduo::net;
using std::unordered_map;
using std::string;
namespace smtp = libcurlwrappersmtp;

const string http_content =
    "GET http://www.baidu.com HTTP/1.1\r\n"
    "Host: www.baidu.com\r\n"
    "User-Agent: inspector(1.1) x86_64 GNU/Linux 5.9.15\r\n"
    "Connection: keep-alive\r\n"
    "Proxy-Authorization: Basic {}\r\n\r\n";

class FlowInspector : noncopyable
{
 public:
  enum atType {
    atType_Offline,
    atType_Unreachable
  };
 public:
  FlowInspector(EventLoop *loop, const InetAddress &servAddr);
  ~FlowInspector();
  void connect();

 private:
  void OnConnection(const TcpConnectionPtr& conn);
  void OnMessage(const TcpConnectionPtr& conn, Buffer *buf, Timestamp recvTime);
  void OnRetryConnect();

  void Alert(atType type);
  bool IsValid(const string& buff);
  void reconnect();
  void OnEmailSendComplete(const smtp::Request& req);
  void checkReceive(const TcpConnectionPtr& conn);

 private:
  EventLoop *loop_;
  TcpClient client_;
  unordered_map<string, string> conn_context_;
  smtp::LibCurlWrapperEmail email_client_;
  int retry_count_;
  unordered_map<string, bool> activeConn_;
};

#endif  // FLOW_INSPECTOR_H_
